<?php
/**
 * 《帮助管理》实现类
 *
 * @author 孙晓晔
 * @version $Id: Help.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */
require_once(dirname(__FILE__) . '/CustomerUtil.class.php');

class Help extends CustomerUtil {
	public $tbl = Constant::tbl_c_help;
		
	public $type = array(
		'-' => '请选择',
		'1' => '分类一',
		'2' => '分类二',
		'3' => '分类三',
	
		'9' => '其它',
	);
	
	public $bulletin = array(
		'-' => '请选择',
		'1' => '是',
		'2' => '否',
	
		'9' => '其它',
	);
	
	public $status = array(
		'-' => '请选择',
		'1' => '等待审核',
		'2' => '正在执行',
		'3' => '暂停使用',
		'4' => '停止使用',
	
		'9' => '其它',
	);

	public $orderby = array(
		'-' => '请选择',
		'name' => '标题',
		'type' => '分类',
	
		'1' => '创建时间',
		'2' => '更新时间',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}
	
	function tab($controller, $request) {		
		$data = array(		
			'帮助管理' => array('cc'=>'info.php?do=Help.find', 'js'=>''),
			// '资讯管理' => array('cc'=>'info.php?do=Text.find', 'js'=>''),	
			'常见问题' => array('cc'=>'info.php?do=Faq.find', 'js'=>''),	

			'联系信息' => array('cc'=>'info.php?do=Contact.find', 'js'=>''),
		);
		
		$ret = $this->_tab($data);

		$controller->assign_by_ref('result', $ret);
		
		$controller->display($request, 'tab');
		return true;
	}

	function pretreat($controller) {
		parent::pretreat($controller);

		$controller->assign_by_ref('type_option', $this->type);
		$controller->assign_by_ref('bulletin_option', $this->bulletin);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'customer/help/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}
	
	function _my() {
		if ($this->_is_admin()) {
			return '';
		}
		
		if ($this->_audit('157')) {
			return " visible = 1";
		}

		$emp = $_SESSION['id'];

		return sprintf(" visible = 1 AND (emp = '%s' OR oper = '%s')", $emp, $emp);
	}

	function _where($request) {
		$name_s = $request['name_s'];
		$type_s = $request['type_s'];
		$bulletin_s = $request['bulletin_s'];

		$my = $this->_my();

		if (empty($my)) {
			$where = " WHERE 1=1";
		} else {
			$where = " WHERE " . $my;
		}
		
		if (!empty($name_s)) {
			$where .= " AND (name LIKE '%" . $name_s . "%' OR content LIKE '%" . $name_s . "%')";
		}

		if (!empty($type_s) && $type_s <> '-') {
			$where .= " AND type = '" . $type_s . "'";
		}
		
		if (!empty($bulletin_s) && $bulletin_s <> '-') {
			$where .= " AND bulletin = '" . $bulletin_s . "'";
		}

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'customer/help/list');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);
		
		$controller->assign('t2', strtotime('7 day'));

		$controller->display($request, 'customer/help/add');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);

		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);
		
		$name = $request['name'];
		$type = $request['type'];
		$bulletin = $request['bulletin'];

		$content = addslashes($request['content']);

		$t1 = $request['t1'];
		$t2 = $request['t2'];

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(office, name, type, bulletin, content, t1, t2, emp, oper, createtime, modifytime)";
		$fmt .=" VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $office, $name, $type, $bulletin, $content, $t1, $t2, $emp, $_SESSION['id']);
		debug($sql);
		$db->query($sql);
		
		$request['id_s'] = $db->insert_id();

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);
		
		$orderby = range(0, 12, 1);
		$controller->assign_by_ref('orderby_option', $orderby);

		$controller->display($request, 'customer/help/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);
		
		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);

		$name = $request['name'];
		$type = $request['type'];
		$bulletin = $request['bulletin'];

		$content = addslashes($request['content']);

		$t1 = $request['t1'];
		$t2 = $request['t2'];
		$orderby = $request['orderby'];

		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET name = '%s', name = '%s', type = '%s', bulletin = '%s', content = '%s', t1 = '%s', t2 = '%s', orderby = '%s', ";
		$fmt .= "emp = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $office, $name, $type, $bulletin, $content, $t1, $t2, $orderby, $emp, $id);
		debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function upload($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
		
		$this->_files_clean($id);
		
		$row = $this->_view($id);
	
		$controller->assign_by_ref('result', $row);
	
		$controller->display($request, 'info/help/upload');
		return true;
	}
	
	function copy($controller, $request) {
		$this->pretreat($controller);
	
		$this->findPara($controller, $request);
	
		$id = $request['id'];
		$row = $this->_view($id);
	
		$controller->assign_by_ref('result', $row);
	
		$orderby = range(0, 12, 1);
		$controller->assign_by_ref('orderby_option', $orderby);
	
		$controller->display($request, 'customer/help/copy');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);
		
		$row['files'] = $this->_files($row['id']);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'customer/help/view');
		return true;
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
		
		$row['type_t'] = $this->type[$row['type']];
		$row['bulletin_t'] = $this->bulletin[$row['bulletin']];
		
		$row['files'] = $this->_files($row['id']);
		
		$row = $this->_view_t($row);

		return $row;
	}
	
	function delete($controller, $request) {
		$this->pretreat($controller);
		
		while (list($k, $id) = each($request)) {
			if (substr($k, 0, 2) <> "c_") {
				continue;
			}
			
			$row = $this->_view($id);
			if (!$this->_is_delete($row)) {
				continue;
			}

			$this->_delete3($id);
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function _delete3($id) {
		$db = newdb($this->dsn_w);
		if ($this->_is_admin()) {
			$fmt = "DELETE FROM %s WHERE id = '%s'";
		} else {
			$fmt = "UPDATE %s SET visible = 2 WHERE id = '%s'";
		}
		
		$sql = sprintf($fmt, $this->tbl, $id);
		debug($sql);
		$db->query($sql);
	}
	
	function _is_delete($row) {
		if ($this->_is_admin()) {
			return true;
		}

		if ($row['oper'] == $_SESSION['id']) {
			return true;
		}

		return false;
	}
	
	function ajax_oper($controller, $request) {
		$id = $_COOKIE['id_'];
		$ret = array();
		
		if ($this->_ajax_x($id, 'oper')) {
			$ret['status'] = 1;
		} else if ($this->_ajax_x($id, 'emp')) {
			$ret['status'] = 1;
		} else {
			$ret['status'] = 2;
		}

		echo json_encode($ret);
	}

	/**
	 * 测试
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function t($controller, $request) {
		$mem=new Memcache;
		$mem->connect("192.168.2.122", 11211);

		//保存数据
		$mem->set('key1','This is first value',0,60);
		$val=$mem->get('key1');
		echo "Get key1 value: ".$val."<br>";


		$controller->display($request, 't');
		return true;
	}
}
?>
